package com.pudu.json;

import android.util.Log;

import com.google.gson.Gson;
import com.pudu.mydemo.entity.Point;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

/**
 * Created by Administrator on 2016/8/22 0022.
 */
public class MapDecode {
    private Vector<double[]> uwb = new Vector<double[]>();
    private Vector<Integer> uwbId = new Vector<>();
    private Vector<Double> trackWeight = new Vector<>();
    private Vector<double[]> track = new Vector<double[]>();
    private ArrayList<double[]> target = new ArrayList<>();
    private ArrayList<double[]> source = new ArrayList<>();


    public void decode(String string) throws JSONException {
        Log.i("MapDecode","string:"+ string);
        JSONObject jsonObject = new JSONObject(string);
        Log.i("MapDecode","jsonObject:" + jsonObject.toString());
        decode(jsonObject);
    }

    public void decode(JSONObject jsonObject) throws JSONException {

        Gson gson = new Gson();
        String string;
        PuduJsonData puduJsonData;

        JSONArray jsonArray = jsonObject.getJSONObject("map").getJSONArray("elements");
        for(int i=0; i<jsonArray.length(); i++) {
            string = jsonArray.getString(i).toString();
            puduJsonData = gson.fromJson(string,PuduJsonData.class);
            if (puduJsonData.getType().equals("uwb")) {
                uwbId.add(puduJsonData.getId());
                uwb.add(puduJsonData.getVector());
            }
            if (puduJsonData.getType().equals("track")) {
                track.add(puduJsonData.getVector());
                trackWeight.add(puduJsonData.getWeight());
            }
            if(puduJsonData.getType().equals("target"))
                target.add(puduJsonData.getVector());
            if(puduJsonData.getType().equals("source"))
                source.add(puduJsonData.getVector());
        }
        for(int i=0;i<uwb.size()-1;i++) {
            for(int j=i+1;j<uwb.size();j++) {
                if (uwbId.get(j) < uwbId.get(i)) {
                    int id = uwbId.get(j);
                    uwbId.set(j,uwbId.get(i));
                    uwbId.set(i,id);
                    double [] vector = new double[3];
                    System.arraycopy(uwb.get(j),0,vector,0,3);
                    System.arraycopy(uwb.get(i),0,uwb.get(j),0,3);
                    System.arraycopy(vector,0,uwb.get(i),0,3);
                }
            }
        }
    }

    public double[] getUWBArray() {

        double [] data = new double[uwb.size()*3];

        for(int i=0; i<uwb.size(); i++) {
            data[i*3 + 0] = uwb.get(i)[0];
            data[i*3 + 1] = uwb.get(i)[1];
            data[i*3 + 2] = uwb.get(i)[2];
        }

        Log.i("MapDecode","uwb:"+ Arrays.toString(data));
        return data;
    }

    public double[] getTrackArray() {

        double [] data = new double[track.size()*3];

        for(int i=0; i<track.size(); i++) {
            data[i*3 + 0] = track.get(i)[0];
            data[i*3 + 1] = track.get(i)[1];
            data[i*3 + 2] = 0;
        }

        Log.i("MapDecode","track:"+ Arrays.toString(data));
        getTrackWeightArray();
        return data;
    }

    public double[] getTrackWeightArray() {
        double [] data = new double[trackWeight.size()];
        for(int i=0; i<trackWeight.size(); i++)
            data[i] = trackWeight.get(i);

        Log.i("MapDecode","trackWeight: " + Arrays.toString(data));
        return data;
    }

    public double[] getSource() {
        double [] data = new double[source.size() * 2];
        for (int i=0; i<source.size(); i++) {
            data[i*2 + 0] = source.get(i)[0];
            data[i*2 + 1] = source.get(i)[1];
        }

        Log.i("MapDecode","Source:"+ Arrays.toString(data));
        return data;
    }

    public List<Point> getTrackList() {
        List<Point> points = new ArrayList<Point>();

        double[] data  = new double[4];
        if(track.size() <= 0) {
            return null;
        } else {
            for (int i = 0 ; i<track.size(); i++) {
                 data = track.get(i);
                Log.e("Diag","data:"+data[0] );
                Point point = new Point();

                if(data[0] > data[2]) {
                    point.setStartX((float) data[2]);
                    point.setStartY((float)data[3]);
                    point.setEndX((float)data[0]);
                    point.setEndY((float)data[1]);
                } else {
                    point.setStartX((float) data[0]);
                    point.setStartY((float)data[1]);
                    point.setEndX((float)data[2]);
                    point.setEndY((float)data[3]);
                }
                 points.add(point);
            }
        }
        return points;
    }

    public List<Point> getUwbList() {
        List<Point> points = new ArrayList<Point>();

        double[] data  = new double[3];
        if(uwb.size() <= 0) {
            return null;
        } else {
            for (int i = 0 ; i<uwb.size(); i++) {
                data = uwb.get(i);
                Log.e("Diag","data:"+data[0] );
                Point point = new Point();
                point.setStartX((float) data[0]);
                point.setStartY((float)data[1]);
                point.setEndX((float) data[0]);
                point.setEndY((float) data[1]);
                points.add(point);
            }
        }
        return points;
    }

    public List<Point> getTargetList() {
        List<Point> points = new ArrayList<>();
        double[] data = new double[2];
        if(target.size() <= 0){
            return null;
        }
        for(int i=0; i<target.size(); i++) {
            data = target.get(i);
            Point point = new Point();
            point.setStartX((float)data[0]);
            point.setStartY((float)data[1]);
            point.setEndY((float)data[1]);
            point.setEndX((float)data[0]);
            points.add(point);
        }
        return points;
    }

}
